<template>
  <el-dialog :title="isEdit ? '修改账户信息': '新增账户信息'" :visible.sync="visible" :close-on-click-modal="closeOnClickModal" @open="_handleDialogOpen" @close="_cancel">
    <el-form ref="dataForm" :model="form" :status-icon="true" :rules="formRules" label-width="100px">
      <el-form-item label="合伙人" prop="name">
        <el-select v-model="form.userid" style="width:100%" placeholder="请选择" @change="setName()">
          <el-option
            v-for="item in partnerList"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item label="账户名称">
        <el-input
          v-model.trim="form.name"
          :placeholder="`请输入账户名称`"
          maxlength="20"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="_cancel">取 消</el-button>
      <el-button :loading="_loading" size="small" type="primary" @click="_ok">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getUserById } from '@/api/systemManage/user'
import { getPartnerListNoPage } from '@/api/systemManage/partner'
import waves from '@/directive/waves' // Waves directive
import defaultHeadImg from '@/assets/defaultHeadImg.png'
import { mapGetters } from 'vuex'

export default {
  directives: { waves },
  components: {
    // DetailCropper
  },

  props: {
    closeOnClickModal: {
      type: Boolean,
      default: false
    },

    loading: {
      type: Boolean,
      default: false
    }
  },

  data() {
    return {
      visible: false,
      isEdit: false,
      partnerList: [],
      form: {},
      formRules: {
        name: [{
          required: true,
          message: '请填写账户名称'
        }]
      }
    }
  },

  computed: {

    ...mapGetters([
      'lang'
    ]),

    validatePassword() {
      return this.form.password === this.defaultPassword
    },

    defaultHeadImg() { return defaultHeadImg },

    '_loading': {
      get() {
        return this.loading
      },

      set(value) {
        this.$emit('update:loading', value)
      }
    }
  },

  methods: {
    setName(e) {

    },

    _formReset() {
      this.form = {
        name: '',
        userid: ''
      }
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields()
      })
    },

    _handleDialogOpen() {
      this._formReset()
      this.$emit('open')
      this.getPartner()
    },

    getPartner() {
      getPartnerListNoPage({ isLeader: 1 }).then(resp => {
        this.partnerList = resp.data.list
        console.log(this.partnerList)
      }).catch(resp => {
        this.$message.error(resp.message)
      })
    },

    _cancel() {
      this._formReset()
      this.visible = false
      this.isLoading = false
      this.$emit('cancel')
    },

    _ok() {
      const form = this.$refs['dataForm']
      form.validate(valid => {
        if (valid) {
          const formData = JSON.parse(JSON.stringify(this.form))
          console.log(formData)
          this.$emit('ok', formData, this.isEdit)
        }
      })
    },

    show(params) {
      this.visible = true
      if (params) {
        this.isEdit = true
        getUserById(params.id || params).then(resp => {
          const data = resp.data
          this.form = data
        }).catch(resp => {
          this.$message.error(resp.message)
        })
      } else {
        this.isEdit = false
      }
    },

    close() {
      this._cancel()
    }
  }
}
</script>
